package org.setamv.springcloud.ribbon.raw;

import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import org.setamv.springcloud.ribbon.rule.MyLoadBalanceRule;

import java.util.Arrays;
import java.util.List;

/**
 * ribbon的负载均衡器的演示
 */
public class LoaderBalancerStarter {

    /**
     * 下面通过纯代码的方式使用ribbon的负载均衡器
     * @throws Exception
     */
    public static void  main(String[] args) throws Exception {
        ILoadBalancer loadBalancer = new BaseLoadBalancer();
        List<Server> servers = Arrays.asList(
                new Server("www.setamv.tech", 9091),
                new Server("www.setamv.tech", 9092)
        );

        loadBalancer.addServers(servers);

        // 设置负载均衡器的规则
        IRule rule = new MyLoadBalanceRule();
        ((BaseLoadBalancer)loadBalancer).setRule(rule);

        for (int i = 0; i < 10; i++) {
            Server server = loadBalancer.chooseServer(null);
            System.out.println(server);
        }
    }
}
